<?php

  /* 
   * @param: $id: image id 
   * @param: $image_sequence: upload an image will generate multiple image. This sequence number indicate the sequence of image being generated. 
   *         0: original image, 
   *         1: thumbnail, 
   *         2: medium etc
   *     
   * @param: $max_dim (append image max_dim after the base path)
   *          
   * @param: $path_by_date: part of the image saving path is by upload date
   *         1: not use path by date, 2: yy/mm/dd/hh 3: yyyy/mm/dd/hh 
   *         4: yy/mm/dd, 5: yyyy/mm/dd, 
   *         6: yy/mmdd   7: yyyy/mmdd
   *         8: yy/mm,    9: yyyy/mm, 
   *         10: yy,      11: yyyy
   *         12: unix timestamp
   *       
   * @param: $path_by_id: 
   *         1: not use,
   *         2: as_id
   *         3: (100 files, 2 digit filename)   ff  (under 100)   
   *         4: (1000 flies, 3 digits filename) fff
   *         5: (100x100 files,   2 digits folder, 2 digits filename) dd/ff
   *         6: (100x100 files,   2 digits 1st folder, 2digits 2nd folder)  dd/dd/
   *         7: (1000x1000 files, 3 digits folder, 3 digits filename)       ddd/fff  (folder:000-999, file:000-999, number of file in each folder: 1000 * max_image_seq), 
   *         8: (1000x1000 files, 3 digits 1st folder, 3 digits 2nd folder) ddd/ddd/ (1st folder: 000-999, 2nd level folder: 000-999, under 2nd folder, image_sequence, number of files supported is same as mode 2) 
   *         9: (1000x1000x1000 files, 3 digits 1st folder, 3 digits 2nd folder, 3 digits filename) ddd/ddd/fff
   *        10: (1000x1000x1000 files, 3 digits 1st folder, 3 digits 2nd folder, 3 digits 3rd level) ddd/ddd/ddd/
   *        more to add..
   *
   * @param: $default_filename string
   *         could be nothing, 
   *         if folder is required, it can be something like '/p'
   * @param: $append_updatetime
   *         1: not use
   *         2: 3 digits last modify
   *         3: 4 digits last modify (update time)
   * 
   * $path_by_date        $path_by_id         $image_sequence        $default_filename        $append_updatetime
   * yy/mmdd              as_id                                    p                         not use
   *       10/0808/120/0p.jpg, 
   *       10/0808/120/1p.jpg,
   *
   */
  function get_upolad_image_path($id, $image_sequence, $h_param=array()){
    # $max_dim, $path_by_date=>1, $path_by_id=>1, $default_filename=>'', $append_updatetime=>1
    # unix_timestamp =>  1281238599, #for append_updatetime
    # datetime       =>  2010/08/08, #for path_by_date
    # 
    
    # need to use isset as all parameter in $h_param is optional
    $a_path = array();
    if (isset($h_param['max_dim'])      && $h_param['max_dim'] > 1      )  
       array_push($a_path, $h_param['max_dim']);
       
    if (isset($h_param['path_by_date']) && $h_param['path_by_date'] > 1 )  
       array_push($a_path, get_path_by_date($h_param['path_by_date'], isset($h_param['datetime'])?$h_param['datetime']:''  ));
       
    if (isset($h_param['path_by_id'])   && $h_param['path_by_id'] > 1   )  
       array_push($a_path, get_path_by_id($h_param['path_by_id'], $id));

    $a_filename = array();     
    if (isset($h_param['default_filename']) ){  
      array_push($a_filename, get_default_image_filename($image_sequence, $h_param['default_filename']));
    } else {
      array_push($a_filename, get_default_image_filename($image_sequence));
    }
    
    if (isset($h_param['append_updatetime']) && $h_param['append_updatetime'] > 1)  
      array_push($a_filename, get_path_by_updatetime($h_param['append_updatetime'], 
                 isset($h_param['unix_timestamp'])?$h_param['unix_timestamp']:''));    
            
    $filename = implode('_', $a_filename); # filename without ext                                     
    return implode('/', $a_path).$filename;
  }

  // path for upload file, detail pls refer to get_upload_image_path
  function get_upolad_path($id, $h_param=array()){
    $a_path = array();
    if (isset($h_param['max_dim'])      && $h_param['max_dim'] > 1      )  
       array_push($a_path, $h_param['max_dim']);
       
    if (isset($h_param['path_by_date']) && $h_param['path_by_date'] > 1 )  
       array_push($a_path, get_path_by_date($h_param['path_by_date'], isset($h_param['datetime'])?$h_param['datetime']:''  ));
       
    if (isset($h_param['path_by_id'])   && $h_param['path_by_id'] > 1   )  
       array_push($a_path, get_path_by_id($h_param['path_by_id'], $id));

    $a_filename = array();     
    if (isset($h_param['default_filename']) )  
      array_push($a_filename, $h_param['default_filename']);
      
    if (isset($h_param['append_updatetime']) && $h_param['append_updatetime'] > 1 )  
      array_push($a_filename, get_path_by_updatetime($h_param['append_updatetime'], 
                 isset($h_param['unix_timestamp'])?$h_param['unix_timestamp']:''));    
                 
    $filename = implode('_', $a_filename); # filename without ext                                     
    return implode('/', $a_path).$filename;
    
  }

  /*         1: not use path by date, 2: yy/mm/dd/hh 3: yyyy/mm/dd/hh 
   *         4: yy/mm/dd, 5: yyyy/mm/dd, 
   *         6: yy/mmdd   7: yyyy/mmdd
   *         8: yy/mm,    9: yyyy/mm, 
   *         10: yy,       11: yyyy
   *         12: unix timestamp
   *   Y: A full numeric representation of a year, 4 digits
   *   y: A two digit representation of a year
   *   m: Numeric representation of a month, with leading zeros
   *   d: Day of the month, 2 digits with leading zeros
   *   H: 24-hour format of an hour with leading zeros
   */  
  function get_path_by_date($path_by_date, $datetime=''){
    if ( $path_by_date <= 1) return '';
    switch ($path_by_date){
      case 2: return date('y/m/d/H');
      case 3: return date('Y/m/d/H');
      case 4: return date('y/m/d');
      case 5: return date('Y/m/d');
      case 6: return date('y/md');
      case 7: return date('Y/md');
      case 8: return date('y/m');
      case 9: return date('Y/m');
      case 10: return date('y');
      case 11: return date('Y');
      #case 12: return
    }
  }

  /*         1: not use,
   *         2: as_id
   *         3: (100 files, 2 digit filename)   ff  (under 100)   
   *         4: (1000 flies, 3 digits filename) fff
   *         5: (100x100 files,   2 digits folder, 2 digits filename) dd/ff
   *         6: (100x100 files,   2 digits 1st folder, 2digits 2nd folder)  dd/dd/
   *         7: (1000x1000 files, 3 digits folder, 3 digits filename)       ddd/fff  (folder:000-999, file:000-999, number of file in each folder: 1000 * max_image_seq), 
   *         8: (1000x1000 files, 3 digits 1st folder, 3 digits 2nd folder) ddd/ddd/ (1st folder: 000-999, 2nd level folder: 000-999, under 2nd folder, image_sequence, number of files supported is same as mode 2) 
   *         9: (1000x1000x1000 files, 3 digits 1st folder, 3 digits 2nd folder, 3 digits filename) ddd/ddd/fff
   *        10: (1000x1000x1000 files, 3 digits 1st folder, 3 digits 2nd folder, 3 digits 3rd level) ddd/ddd/ddd/
   */   
  function get_path_by_id($path_by_id, $id){
    if ( $path_by_id <= 1) return '';
    
    switch ( $path_by_id ){
      case 2: return $id;
      case 3: return str_pad( $id, 2, '0', STR_PAD_LEFT );
      case 4: return str_pad( $id, 3, '0', STR_PAD_LEFT );
      case 5: 
        $id_padded = str_pad( $id, 4, '0', STR_PAD_LEFT );
        return substr($id_padded, 0, -2).'/'.substr($id_padded, -2);
      case 6: 
        $id_padded = str_pad( $id, 4, '0', STR_PAD_LEFT );
        return substr($id_padded, 0, -2).'/'.substr($id_padded, -2).'/';
      case 7: 
        $id_padded = str_pad( $id, 6, '0', STR_PAD_LEFT );
        return substr($id_padded, 0, -3).'/'.substr($id_padded, -3);
      case 8: 
        $id_padded = str_pad( $id, 6, '0', STR_PAD_LEFT );
        return substr($id_padded, 0, -3).'/'.substr($id_padded, -3).'/';
      case 9: 
        $id_padded = str_pad( $id, 9, '0', STR_PAD_LEFT );
        return substr($id_padded, 0, -6).'/'.substr($id_padded, -6, 3).'/'.substr($id_padded, -3);
      case 10: 
        $id_padded = str_pad( $id, 9, '0', STR_PAD_LEFT );
        return substr($id_padded, 0, -6).'/'.substr($id_padded, -6, 3).'/'.substr($id_padded, -3);
    }
  }
  
  function get_default_image_filename($image_sequence, $default_filename=''){
    if (empty($default_filename)){
      return $image_sequence;
    }
    
  	if ( substr($default_filename, 0, 1) == '/' ){
      return $image_sequence.$default_filename;
    }
    
    return $image_sequence.'_'.$default_filename;
  }
  
  
  function get_path_by_updatetime($append_updatetime, $unix_timestamp=''){
    if ($append_updatetime == 1) return '';
    
    if (empty($unix_timestamp)){
      $unix_timestamp = time();
    }
    
    switch($append_updatetime){
      case 2: return substr($unix_timestamp, -3);
      case 3: return substr($unix_timestamp, -4);
      default:
        return substr($unix_timestamp, -4);  
    }    
  }
  
  
  
  
  
  
?>